package com.jt.service;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.jt.mapper.OrderItemMapper;
import com.jt.mapper.OrderMapper;
import com.jt.mapper.OrderShippingMapper;
import com.jt.pojo.Order;
import com.jt.pojo.OrderItem;
import com.jt.pojo.OrderShipping;

@Service
public class DubboOrderServiceImpl implements DubboOrderService {
	
	@Autowired
	private OrderMapper orderMapper;
	@Autowired
	private OrderShippingMapper orderShippingMapper;
	@Autowired
	private OrderItemMapper orderItemMapper;


	@Override
	public String saveOrder(Order order) {
		//1.拼接OrderId
		String orderId =
				"" + order.getUserId() + System.currentTimeMillis();
		//2.完成订单入库
		order.setOrderId(orderId).setStatus(1);
		orderMapper.insert(order);

		//3.完成订单物流入库
		OrderShipping orderShipping = order.getOrderShipping();
		orderShipping.setOrderId(orderId);
		orderShippingMapper.insert(orderShipping);

		//4.完成订单商品入库
		List<OrderItem> orderItems = order.getOrderItems();
		//批量入库  sql: insert into xxx(xxx,xx,xx)values (xx,xx,xx),(xx,xx,xx)....
		for (OrderItem orderItem : orderItems){
			orderItem.setOrderId(orderId);
			orderItemMapper.insert(orderItem);
		}
		System.out.println("订单入库成功!!!!");
		return orderId;
	}

	@Override
	public Order findOrderById(String id) {
		//1.查询订单信息
		Order order  = orderMapper.selectById(id);
		//2.查询订单物流信息
		OrderShipping orderShipping = orderShippingMapper.selectById(id);
		//3.查询订单商品
		QueryWrapper<OrderItem> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("order_id",id);
		List<OrderItem> lists =orderItemMapper.selectList(queryWrapper);
		return order.setOrderItems(lists).setOrderShipping(orderShipping);
	}
}

